package offer;

/**
 * @Author SSK
 * @Date Created in 17:13 2019/8/6
 * @Description 表示数值的字符串
 * @Version 1.0
 */
public class T54 {
    public boolean isNumeric(char[] str) {
        return new String(str).matches("^[\\+\\-]?[0-9]*(\\.[0-9]+)?([Ee][\\+\\-]?[0-9]+)?$");
    }
    public boolean isNumeric2(char[] str) {
       int d = 0;
       int e = 0;
       int start = 0;
       if (str[0]=='+'||str[0]=='-') {
           start++;
       }
        for (int i = start; i < str.length; i++) {
            if (str[i]=='.') {
                d++;
            } else if (str[i]>='0'&&str[i]<='9') {
                while ( i < str.length && str[i]>='0'&&str[i]<='9') {
                    i++;
                }
                i--;
            } else if (str[i]=='E'||str[i]=='e') {
                   if (i==0) {
                       return false;
                   }
                   if (str[i-1]=='.'||i==str.length-1) {
                       return false;
                   }
                   i++;
                   if (str[i]=='-'||str[i]=='+') {
                       if (i==str.length-1) {
                           return  false;
                       }
                       i++;
                   }
                   while ( i < str.length && str[i]>='0'&&str[i]<='9') {
                       i++;
                   }

                   if (i!=str.length) {
                       return false;
                   }
            } else {
                return false;
            }
        }
        if (d<2) {
            return true;
        }
        return false;
    }
    public static void main ( String[] args) {

        System.out.println(new T54().isNumeric2("123e".toCharArray()));
    }
}
